const xlsx = require('node-xlsx');
const fs = require("fs");
const puppeteer = require('puppeteer');
const dateFormat = require('./util/date.js');
const replite = require('./reptiles.js');
void async function() {

    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    const workSheetsFromFile = xlsx.parse(`${__dirname}/cherrio.xlsx`);
    let workSheetsArr = workSheetsFromFile[0].data;
    workSheetsArr = workSheetsArr.filter(item=> {
        if(item.length == 2 && item[1].includes('onlyfans.com')) {
            if(item[1].startsWith('onlyfans.com') || item[1].startsWith("www")) {
                item[1] = "https://" + item[1];
            }
            if(item[1].startsWith('Onlyfans.com/')) {
                item[1] = "https://onlyfans.com" + (item[1].split('Onlyfans.com/')[1])
            }
            return item
        }
    })
    
    let urlMap = {};
    // filter
    workSheetsArr.forEach(item=> {
        urlMap[item[1]] = item[1];
    })

    let dataArr = [];
    dataArr.push([
        "url",
        "nickName",
        "posts",
        "likes",
        "lastSeen",
        "userInfo",
        "avatar",
        "time"
    ])
    let arr = Object.values(urlMap)
    for(item of arr) {
        if(item.includes("onlyfans.com")) {
            let result = {};
            result = await replite(page,item)
            dataArr.push(Object.values(result))
            var buffer = xlsx.build([{name: `onlyfans`, data: dataArr}]);
            fs.writeFileSync(`onlyfans_${dateFormat(new Date(),"yyyy_MM_dd")}.xlsx`,buffer);
        }
    }
    browser.close()
}();
